package com.open.base.service.impl;


import com.open.base.mapper.GeneralConfigMapper;
import com.open.base.model.GeneralConfig;
import com.open.base.model.vo.SysConfigVo;
import com.open.base.service.IGeneralConfigService;
import com.open.common.model.Page;
import com.open.common.model.Sort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 通用配置
 *
 * @author 大树03
 * @date 2025-03-31 21:00
 */
@Service(value = "userConfigService")
@Slf4j
public class GeneralConfigServiceImpl implements IGeneralConfigService {

	private GeneralConfigMapper generalConfigMapper;

    @Autowired
    public void setUserConfigMapper(GeneralConfigMapper generalConfigMapper) {
        this.generalConfigMapper = generalConfigMapper;
    }

    @Override
    @Transactional()
    public Boolean save(GeneralConfig model)  {
        return generalConfigMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(GeneralConfig model)  {
        return generalConfigMapper.update(model);
    }

	@Override
	public Page<SysConfigVo> findPage(Page<GeneralConfig> configPage, GeneralConfig item)  {
        Page<SysConfigVo> page = new Page<SysConfigVo>();
        BeanUtils.copyProperties(configPage, page);
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
        map.put("endPoint",page.getSize());
        Integer count = generalConfigMapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(generalConfigMapper.findPage(map));
        }else {
            page.setItems(new ArrayList<SysConfigVo>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<GeneralConfig> findByModel(GeneralConfig item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return generalConfigMapper.findByModel(map);
    }

	@Override
	public GeneralConfig findByPk(Long pk)  {
        return generalConfigMapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(GeneralConfig item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return generalConfigMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(List<Long> pks) {
        for (Long pk : pks) {
            generalConfigMapper.remove(pk);
        }
        return true;
    }

    @Override
    public GeneralConfig findConfig(Long userId, String type) {
        return generalConfigMapper.findConfig(userId, type);
    }

    @Override
    public String getSystemConfig(String type) {
        return generalConfigMapper.getSystemConfig(type);
    }

    @Override
    public List<String> getSystemConfigs(List<String> types) {
        // 获取查询结果
        List<Map<String, Object>> configMaps = generalConfigMapper.getSystemConfigs(types);
        
        // 创建一个Map用于快速查找
        Map<String, String> configLookup = new HashMap<>();
        for (Map<String, Object> configMap : configMaps) {
            String type = (String) configMap.get("type");
            String config = (String) configMap.get("config");
            configLookup.put(type, config);
        }
        
        // 按照传入的types顺序构建结果列表
        List<String> orderedConfigs = new ArrayList<>();
        for (String type : types) {
            String config = configLookup.get(type);
            orderedConfigs.add(config); // 如果没找到会添加null，保持位置一致
        }
        
        return orderedConfigs;
    }

    @Override
    public Boolean batchUpdate(List<GeneralConfig> models) {
        return generalConfigMapper.batchUpdate(models);
    }
}